package com.tianji.learning.listener;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class LessonChangeListener {

    private final ILearningLessonService iLearningLessonService;

    /**
     * 业务实现-添加课程到课表
     *
     * @param dto
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("add.queue"),
            exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE, type = "topic"),
            key = MqConstants.Key.ORDER_PAY_KEY
    ))
    public void addLesson(OrderBasicDTO dto) {
        if (dto == null || dto.getOrderId() == null || CollUtils.isEmpty(dto.getCourseIds())) {
            log.info("订单信息有误!!!");
            return;
        }
        iLearningLessonService.addLesson(dto);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("refund.queue"),
            exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE, type = "topic"),
            key = MqConstants.Key.ORDER_REFUND_KEY
    ))
    public void deleteLearningLesson(OrderBasicDTO dto) {
        if (dto == null || dto.getOrderId() == null || CollUtils.isEmpty(dto.getCourseIds())) {
            log.info("订单信息有误!!!");
            return;
        }
        iLearningLessonService.deleteLearningLesson(dto);
    }
}
